#include <iostream>
using namespace std;

class Singleton
{
    class AutoRelease{
      public: AutoRelease(){}
              //通过静态成员_ar确保程序结束的时候自动调用析构函数回收pInstance
              ~AutoRelease(){
                  cout <<"~AutoRelease()"<<endl;
                  if(_pInstance){
                     delete  _pInstance;
                     _pInstance = nullptr;
                  }
              }

    };
public:
    static Singleton *getInstance(){
        if(_pInstance == nullptr){
            _pInstance = new Singleton();
        }
        return _pInstance;
    }
private:
    Singleton(){}
    ~Singleton(){}
    static Singleton * _pInstance;
    static AutoRelease _ar;
};
Singleton* Singleton::_pInstance = nullptr;
Singleton::AutoRelease Singleton:: _ar;
void test(){
  Singleton::getInstance();
}
int main()
{
    test();
    return 0;
}

